<?php

class MemberPairingBV extends DataExtension {
	private static $casting = array(
        'AccumulateLeftGroupBV' => 'Int',
        'AccumulateRightGroupBV' => 'Int',
        'TodayLeftGroupBV' => 'Int',
        'TodayRightGroupBV' => 'Int',
        'CarryLeftGroupBV' => 'Int',
        'CarryRightGroupBV' => 'Int',
        'TodayTotalLeftGroupBV' => 'Int',
        'TodayTotalRightGroupBV' => 'Int'
    );
	
	function getAccumulateLeftGroupBV(){
		$summary = BonusSummary::get()->filter('MemberID', $this->owner->ID)->innerJoin('BonusBatch', 'BonusBatch.ID = BonusSummary.BatchID')->sort('BonusBatch.DateEnd', 'DESC')->first();
		return $summary ? $summary->ALPGBV : 0;
	}
	
	function getAccumulateRightGroupBV(){
		$summary = BonusSummary::get()->filter('MemberID', $this->owner->ID)->innerJoin('BonusBatch', 'BonusBatch.ID = BonusSummary.BatchID')->sort('BonusBatch.DateEnd', 'DESC')->first();
		return $summary ? $summary->ARPGBV : 0;
	}
	
	function getTodayLeftGroupBV(){
		$bv = DB::query("SELECT sum(o.BV) FROM Placement p inner join Placement d on d.NLeft >= p.NLeft AND d.NRight <= p.NRight inner join OrderPackage o on o.MemberID = d.MemberID AND o.Status = 'Fully Paid' WHERE o.BatchID = '".BonusBatch::get_current_batch_id()."' AND p.ParentID = '".$this->owner->ID."' AND p.NSeqno = 1")->value();
		return $bv ? $bv : 0;
	}
	
	function getTodayRightGroupBV(){
		$bv = DB::query("SELECT sum(o.BV) FROM Placement p inner join Placement d on d.NLeft >= p.NLeft AND d.NRight <= p.NRight inner join OrderPackage o on o.MemberID = d.MemberID AND o.Status = 'Fully Paid' WHERE o.BatchID = '".BonusBatch::get_current_batch_id()."' AND p.ParentID = '".$this->owner->ID."' AND p.NSeqno = 2")->value();
		return $bv ? $bv : 0;
	}
	
	function getCarryLeftGroupBV(){
		$summary = BonusSummary::get()->filter('MemberID', $this->owner->ID)->innerJoin('BonusBatch', 'BonusBatch.ID = BonusSummary.BatchID')->sort('BonusBatch.DateEnd', 'DESC')->first();
		return $summary ? $summary->CFLPGBV : 0;
	}
	
	function getCarryRightGroupBV(){
		$summary = BonusSummary::get()->filter('MemberID', $this->owner->ID)->innerJoin('BonusBatch', 'BonusBatch.ID = BonusSummary.BatchID')->sort('BonusBatch.DateEnd', 'DESC')->first();
		return $summary ? $summary->CFRPGBV : 0;
	}

	function getTodayTotalLeftGroupBV(){
		return $this->getCarryLeftGroupBV() + $this->getTodayLeftGroupBV();
	}
	
	function getTodayTotalRightGroupBV(){
		return $this->getCarryRightGroupBV() + $this->getTodayRightGroupBV();
	}
}
?>